package com.example.wan1.adapter;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.wan1.R;
import com.example.wan1.model.ItemBean;
import com.example.wan1.utils.SPUtils;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.setting.DataCleanManager;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.SwitchCompat;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

/***
 * 创建时间：2020/7/28 20:11
 * 创建人：10850
 * 功能描述：
 */
public class ItemAdapter extends BaseQuickAdapter<ItemBean, BaseViewHolder> {

    public ItemAdapter(int layoutResId, @Nullable List<ItemBean> data) {
        super(layoutResId, data);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    //@SuppressLint("ResourceAsColor")
    @Override
    protected void convert(@NonNull BaseViewHolder holder, ItemBean itemBean) {
        holder.setText(R.id.mine_item_tv, itemBean.getTitle());
        TextView tv = holder.getView(R.id.mine_item_tv);
        ImageView iv = holder.getView(R.id.arrow_right_iv);
        SwitchCompat compat = holder.getView(R.id.mine_item_toggle);

        holder.setVisible(R.id.mine_item_right_tv, false);
        holder.setVisible(R.id.arrow_right_iv, true);
        holder.setVisible(R.id.mine_item_toggle, false);
        holder.setImageResource(R.id.arrow_right_iv, R.drawable.ic_chevron_right_24dp);
        holder.setChecked(R.id.mine_item_toggle, false);
        //compat.setTrackTintList(ColorStateList.valueOf(ToolsUtils.getAttrValue(mContext)));
        //compat.setThumbTintList(ColorStateList.valueOf(ToolsUtils.getAttrValue(mContext)));

        int color = ToolsUtils.getThemeColor(mContext);

        if (itemBean.getType() == 0) {//分类标题
            //tv.setTextColor(R.color.qc_green);//因为这里加上了@SuppressLint("ResourceAsColor")，所以导致颜色不好
            int attrValue = ToolsUtils.getAttrValue(mContext);
            tv.setTextColor(attrValue);
            iv.setVisibility(View.GONE);
            if (color != 0) {
                tv.setTextColor(color);
            }
        } else if (itemBean.getType() == 1) {//正常的标题，显示右箭头
            Drawable drawable = null;
            if (itemBean.getTitle().equals("积分排行榜"))
                drawable = mContext.getResources().getDrawable(R.drawable.ic_rank);
            else if (itemBean.getTitle().equals("我的积分"))
                drawable = mContext.getResources().getDrawable(R.drawable.ic_coin);
            else if (itemBean.getTitle().equals("设置"))
                drawable = mContext.getResources().getDrawable(R.drawable.ic_settings_24dp);

            if (drawable != null) {
                //drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                drawable.setBounds(0, 0, 45, 45);
                tv.setCompoundDrawables(drawable, null, null, null);
                tv.setCompoundDrawablePadding(8);
            }
        } else if (itemBean.getType() == 2) {//Toggle标题
            holder.setVisible(R.id.mine_item_toggle, true);
            holder.setVisible(R.id.arrow_right_iv, false);
            if (itemBean.isCheck()) {
                holder.setChecked(R.id.mine_item_toggle, true);
                if (color != 0) {
                    //compat.setTrackTintList(ColorStateList.valueOf(color, Color.GRAY));
                    compat.setThumbTintList(ColorStateList.valueOf(color));
                }
            } else {
                holder.setChecked(R.id.mine_item_toggle, false);
            }
        } else if (itemBean.getType() == 3) {
            holder.setVisible(R.id.mine_item_right_tv, true);
            if (itemBean.getTitle().equals("清除缓存")) {
                try {
                    holder.setText(R.id.mine_item_right_tv, DataCleanManager.getTotalCacheSize(mContext));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (itemBean.getTitle().equals("夜间模式")) {
                int i = (int) SPUtils.get(mContext, "night_mode_id", 1);
                if (i == MODE_NIGHT_YES) {
                    holder.setText(R.id.mine_item_right_tv, "打开");
                } else if (i == MODE_NIGHT_NO) {
                    holder.setText(R.id.mine_item_right_tv, "关闭");
                } else if (i == MODE_NIGHT_AUTO_BATTERY) {
                    holder.setText(R.id.mine_item_right_tv, "省电模式");
                } else if (i == MODE_NIGHT_FOLLOW_SYSTEM) {
                    holder.setText(R.id.mine_item_right_tv, "跟随系统");
                }
            } else
                holder.setText(R.id.mine_item_right_tv, ToolsUtils.getVersion(mContext));
        } else if (itemBean.getType() == 4) {
            holder.setImageResource(R.id.arrow_right_iv, R.drawable.theme_color_ball);
            if (color != 0) {
                ImageView ivBall = holder.getView(R.id.arrow_right_iv);
                ivBall.setColorFilter(color);
            }
        }

        holder.addOnClickListener(R.id.mine_item_toggle);

    }
}
